/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.*;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import model.*;

/**
 *
 * @author shinji
 */
@Named(value = "a_languageManagedBean")
@SessionScoped
public class LanguageManagedBean implements Serializable {
    @EJB
    private LanguageFacade languageFacade;
    
    private List<Language> list = new ArrayList<>();
    private Language obj = new Language();
    private String Name;

    public List<Language> getList() {
        return list;
    }

    public void setList(List<Language> list) {
        this.list = list;
    }

    public Language getObj() {
        return obj;
    }

    public void setObj(Language obj) {
        this.obj = obj;
    }

    public String getName() {
        return Name;
    }

    public void setName(String languageName) {
        this.Name = languageName;
    }
    
    /**
     * Creates a new instance of LanguageManagedBean
     */
    public LanguageManagedBean() {
    }
    
    /***********************************************/
    
    public void create(){
        try {
            obj = new Language();
            obj.setLanguage(Name);
            
            this.languageFacade.create(obj);
            set_list();
            Name = "";
            
            set_message(FacesMessage.SEVERITY_INFO, "Create Language", "Create " +obj.getLanguage()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Create Language", "Create " +obj.getLanguage()+ " error !!!");
        }
    }
    
    public void edit(){
        try {
            this.languageFacade.edit(obj);
            
            set_message(FacesMessage.SEVERITY_INFO, "Edit Language", "Edit " +obj.getLanguage()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Edit Language", "Edit " +obj.getLanguage()+ " error !!!");
        }
    }
    
    public void remove(Language l){
        try {
            this.languageFacade.remove(l);
            set_list();
            
            set_message(FacesMessage.SEVERITY_INFO, "Remove Language", "Remove " +l.getLanguage()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Remove Language", "Remove " +l.getLanguage()+ " error !!!");
        }
    }
    
    /***********************************************/
    
    public void set_list(){
        try {
            list = this.languageFacade.findAll();
        } catch (Exception e) {
            list = null;
        }
    }
    
    private void set_message(FacesMessage.Severity severity, String title, String message){
        try {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, message));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error message !!!"));
        }
    }
}
